package com.lz.common.utils;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.google.gson.Gson;

import java.util.Map;

public class MyHttpUtil {
    public static void main(String[] args) {
        method02();
    }

    private static void method01() {
        String jsonInputString = JSONUtil.toJsonStr(JSONUtil.createObj().set("str1", "String").set("str2", "String"));

        // 发送 POST 请求并获取响应
        String response = HttpUtil.createPost("http://127.0.0.1:8080/security/login")
                .header(Header.CONTENT_TYPE, "application/json")
                .body(jsonInputString)
                .execute()
                .body();
        Gson gson = new Gson();
        Map map = gson.fromJson(response, Map.class);
        System.out.println(map.get("data"));
    }

    private static void method02() {
        // 构建表单数据
        String formData = "username=" + "String" + "&password=" + "String";
        // 发送 POST 请求并获取响应
        HttpRequest body = HttpUtil.createPost("http://127.0.0.1:8080/security/login")
                .header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded")
                .body(formData);
        System.out.println(body);
        String response = body.execute()
                .body();
        Gson gson = new Gson();
        Map map = gson.fromJson(response, Map.class);
        System.out.println(map.get("data"));
    }
}
